package cn.js.controller;


import cn.js.dto.User;
import cn.js.feign.UserFeignClient;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/pay")
public class PayController {

    @Resource
    private UserFeignClient userFeignClient;
    @Resource
    private  ObjectMapper objectMapper;

    @GetMapping("/test7")
    public String test5() throws IOException {
        User user = new User();
        user.setName("cyk");
        user.setId(123L);
        List<String> arrayList = new ArrayList<>();
        arrayList.add("aaa");
        arrayList.add("bbb");
        user.setArr(arrayList);
        String userList = userFeignClient.getUserList(user);
        User user2 = objectMapper.readValue(userList, User.class);
        System.out.println(user2);
        return "order ok! \n" + userList;
    }


    @SneakyThrows
    @GetMapping("/test6")
    public String test6() {
        String data = userFeignClient.getData();
        Long finalData = objectMapper.readValue(data, Long.class);
        return "user ok!" + finalData;
    }






    @GetMapping("/test4")
    public String test4(String[] arr) {
        String result = userFeignClient.getArr(arr);
        return "order ok! \n" + result;
    }

    @PostMapping("/test3")
    public String test3(@RequestBody User user) {
        user.setName(user.getName());
        user.setIntro(user.getIntro());
        String userinfo = userFeignClient.getUser(user);
        return "order ok! \n" + userinfo;
    }


    @GetMapping("/test2")
    public String test2(String name) {
        String info = userFeignClient.getName(name);
        System.out.println(info);
        return "order ok! \n" + info;
    }

    @GetMapping("/test1")
    public String test2(Long id) {
        String info = userFeignClient.getInfo(id);
        System.out.println(info);
        return "order ok! \n" + info;
    }

    @GetMapping("/getUserById/{id}")
    public User getUserById(@PathVariable Long id){
        //通过 UserFeignClient Feign的客户端接来调用用户服务，获取User并返回，此时这样的写法才够优雅
        return userFeignClient.getById(id);
    }
}
